package com.keeson.android_cli.util;


public class Crc32 {

    /**
     * crc
     * @param bytes
     * @return
     */
    public static byte[] getCRC32(byte[] bytes) {
        byte[] checksum = new byte[1];
        int sum = 0;
        for(int i=0;i<bytes.length;i++){
            sum += (bytes[i])&0xff;
        }
        checksum[0] = (byte) (255-sum);
        return checksum;
    }



    /***
     * CRC校验是否通过
     * @param srcByte
     * @return
     */
    public static boolean isPassCRC(byte[] srcByte) {
        byte []crc = new byte[srcByte.length-2];
        System.arraycopy(srcByte, 0, crc, 0, srcByte.length-2);
        //LogUtils.e("收到校验数据======", HexDump.toHexString(crc)+"");
        //LogUtils.e("收到数据校验结果======", HexDump.toHexString(getCRC32(crc))+"");
        byte []crcResult = new byte[1];
        System.arraycopy(srcByte, srcByte.length-2, crcResult, 0, 1);
        //LogUtils.e("收到数据校验码======",HexDump.toHexString(crcResult)+"");
        return HexDump.toHexString(getCRC32(crc)).equals(HexDump.toHexString(crcResult));
    }

}
